home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / viewkit / xcontact / parody / parody.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.2 KB  |  213 lines

  1. // parody.h
  2.  
  3. #ifndef PARODY_H
  4. #define PARODY_H
  5.  
  6. #include <fstream.h>
  7. #include <Xm/Xm.h>
  8.  
  9. enum pBool { pFalse, pTrue };
  10.  
  11. // ============================
  12. // min/max macros
  13. // ============================
  14. inline int max(int a, int b)
  15. {
  16.     return a > b ? a : b;
  17. }
  18.  
  19. inline int min(int a, int b)
  20. {
  21.     return a < b ? a : b;
  22. }
  23.  
  24.  
  25. #include "pstrings.h"
  26. #include "btree.h"
  27.  
  28. // ============================
  29. // Object Address
  30. // ============================
  31. typedef NodeNbr ObjAddr;
  32.  
  33. // ============================
  34. // Persistent Object Header Rcd
  35. // ============================
  36. struct ObjectHeader    {
  37.     int classid;        // invariant: class identification
  38.     NodeNbr ndnbr;        // relative node number within object
  39.     ObjectHeader(int cid = 0, NodeNbr nd = 0)
  40.         { classid = cid; ndnbr = nd; }
  41. };
  42.  
  43. // ============================
  44. // the Parody database
  45. // ============================
  46. class Parody : public FileHeader    {
  47.     Index index;               // invariant: the b-tree file
  48.     LinkedListHead objects; // list of instantiated objects
  49.     LinkedListHead btrees;    // the b-trees in the database
  50.     ObjAddr rebuildnode;       // node of object being rebuilt
  51. public:
  52.     Parody(pString name);
  53.     ~Parody();
  54.     void Flush();
  55.     Index& IndexFile()          { return index; }
  56.     LinkedListHead& Btrees()    { return btrees; }
  57.     LinkedListHead& Objects()   { return objects; }
  58.     pBool RebuildingIndexes()
  59.         { return (pBool) (rebuildnode != 0); }
  60.     ObjAddr RebuildNode()       { return rebuildnode; }
  61.     void GetObjectHeader(ObjAddr nd, ObjectHeader &objhdr);
  62.     void RebuildIndexes(ObjAddr nd);
  63. };
  64.  
  65. // =====================================
  66. // Persistent object abstract base class
  67. // =====================================
  68. class Persistent    {
  69.     friend class Key;
  70.     friend class ObjectList;
  71.     ObjectHeader objhdr;
  72.     ObjAddr objectaddress;    // Node address for this object
  73.     Parody& parody;            // database for this object
  74.     int indexcount;            // number of keys in the object
  75.     LinkedListHead keys;       // the keys in the object
  76.     LinkedListHead orgkeys; // original keys in the object
  77.     ObjectList *objectlist; // object list entry this object
  78.     Node *node;                // current node for reading/writing
  79.     int offset;                // current char position
  80.     pBool changed;            // pTrue if user changed the object
  81.     pBool deleted;            // pTrue if user deleted the object
  82.     pBool newobject;        // pTrue if user is adding the object
  83.     // --- pointers to associate keys with objects
  84.     Persistent *oldthispers;
  85.     static Persistent *thispers;
  86.     // ---- methods used from within Persistent class
  87.     void ObjectOut();
  88.     void RecordObject();
  89.     void RemoveObject();
  90.     void AddIndexes();
  91.     void DeleteIndexes();
  92.     void UpdateIndexes();
  93.     void PositionNode();
  94.     void ReadObjectHeader();
  95.     void WriteObjectHeader();
  96.     void SearchIndex(Key *key);
  97.     void ReadDataMembers();
  98.     Btree *FindIndex(Key *key);
  99.     pBool TestRelationships();
  100.     void ScanForward(NodeNbr nd);
  101.     void ScanBackward(NodeNbr nd);
  102. protected:
  103.     Persistent(Parody &db, int cid);
  104.     // ---- derived class methods for reading members
  105.     void ReadObject(void *buf, int length);
  106.     void ReadObject(pString& str);
  107.     void ReadObject( char* str )
  108.         { pString s; ReadObject( s ); strcpy( str, s ); }
  109.     void ReadObject(int& i)
  110.         { ReadObject(&i, sizeof(int)); }
  111.     void ReadObject(Boolean& b)
  112.         { int i; ReadObject( i ); b = i; }
  113.     void ReadObject(long& l)
  114.         { ReadObject(&l, sizeof(long)); }
  115.     void ReadObject(float& f)
  116.         { ReadObject(&f, sizeof(float)); }
  117.     void ReadObject(double& d)
  118.         { ReadObject(&d, sizeof(double)); }
  119.     void ReadObject(ObjAddr& o)
  120.         { ReadObject(&o, sizeof(ObjAddr)); }
  121.     // ---- derived class methods for writing members
  122.     void WriteObject(void *buf, int length);
  123.     void WriteObject(pString& str);
  124.     void WriteObject( const char* str )
  125.         { WriteObject( pString( (char *) str ) ); }
  126.     void WriteObject(int i)
  127.         { WriteObject(&i, sizeof(int)); }
  128.     void WriteObject( Boolean b )
  129.         { WriteObject( int( b ) ); }
  130.     void WriteObject(long l)
  131.         { WriteObject(&l, sizeof(long)); }
  132.     void WriteObject(float f)
  133.         { WriteObject(&f, sizeof(float)); }
  134.     void WriteObject(double d)
  135.         { WriteObject(&d, sizeof(double)); }
  136.     void WriteObject(ObjAddr o)
  137.         { WriteObject(&o, sizeof(ObjAddr)); }
  138.     // --- provided by derived class
  139.     virtual void Write() = 0;
  140.     virtual void Read() = 0;
  141. public:
  142.     virtual ~Persistent();
  143.     // ---- called from derived class's constructor
  144.     void LoadObject(ObjAddr nd = 0);
  145.     // ---- called from derived class's destructor
  146.     void SaveObject();
  147.     // ---------- handle/copy variables
  148.     int count;                    // count of handle objects
  149.     Persistent *objectcopy;    // for multiple instantiations
  150.     // --- class interface methods for modifying database
  151.     pBool AddObject();
  152.     pBool ChangeObject();
  153.     pBool DeleteObject();
  154.     pBool ObjectExists()
  155.         { return (pBool) (objectaddress != 0); }
  156.     // ---- class interface methods for searching database
  157.     void FindObject(Key *key);
  158.     void FirstObject(Key *key = NULL);
  159.     void LastObject(Key *key = NULL);
  160.     void NextObject(Key *key = NULL);
  161.     void PreviousObject(Key *key = NULL);
  162. // UMESH 
  163.     void CurrentObject(Key *key = NULL);
  164.     ObjAddr ObjectAddress() { return objectaddress; }
  165. };
  166.  
  167. // ============================
  168. // list of instantiated objects
  169. // ============================
  170. class ObjectList : public LinkedListEntry    {
  171.     friend Persistent;
  172.     Persistent *object;
  173.     ObjectList(LinkedListHead *ll, Persistent *obj) :
  174.             LinkedListEntry(ll)
  175.         { object = obj; }
  176. };
  177.  
  178. // ==================================
  179. // Handle class for handle/copy idiom
  180. // ==================================
  181. class Handle    {
  182. protected:
  183.     Persistent *body;
  184.     Handle() { body = NULL; }
  185.     virtual ~Handle();
  186.     void ConstructBody(Persistent *pbody);
  187. public:
  188.     Handle(Handle& handle);
  189.     Handle& operator=(Handle& empl);
  190.     // --- class interface methods for modifying database
  191.     pBool AddObject()     { return body->AddObject(); }
  192.     pBool ChangeObject()  { return body->ChangeObject(); }
  193.     pBool DeleteObject()  { return body->DeleteObject(); }
  194.     pBool ObjectExists()  { return body->ObjectExists(); }
  195.     // ---- class interface methods for searching database
  196.     void FindObject(Key *key)
  197.         { body->FindObject(key); }
  198.     void FirstObject(Key *key = NULL)
  199.         { body->FirstObject(key); }
  200.     void LastObject(Key *key = NULL)
  201.         { body->LastObject(key); }
  202.     void NextObject(Key *key = NULL)
  203.         { body->NextObject(key); }
  204.     void PreviousObject(Key *key = NULL)
  205.         { body->PreviousObject(key); }
  206.     ObjAddr ObjectAddress()
  207.         { return body->ObjectAddress(); }
  208. };
  209.  
  210. #include "key.h"
  211.  
  212. #endif
  213.